home *** CD-ROM | disk | FTP | other *** search
- #include <Palettes.h>
- #include <SANE.h>
- #include <QDOffscreen.h>
-
- /* size of the voxel data */
- #define volSize 128
- /* half the size of voxel data */
- #define halfVolSize 64
- /* the radius of the sphere */
- #define sphere_r 60
- /* sphere_r*sphere_r */
- #define volumeMag 3600.0
- /* sqrt(3.0)*sphere_r */
- #define sqrt3r 103.9230
- /* sphere_r*sqrt(6+2*sqrt(3)) */
- #define rsqrt6 184.5827
-
- /* resource numbers for the window, palette and menus */
- #define windowRscr 128
- #define paletteRscr 128
-
- #define appleID 128
- #define appleM 1
- #define appleAbout 1
-
- #define fileID 129
- #define fileM 2
- #define fileQuit 1
-
- #define editID 130
- #define editM 3
- #define editUndo 1
- #define editCut 3
- #define editCopy 4
- #define editPaste 5
- #define editClear 6
-
- #define sleepTicks 30
-
- #define aboutDialog 128
-
- /* these constants define the Phong shading */
- /* ambient reflection coefficient */
- #define ambientReflCoef 0.1
- /* depth cueing coefficient */
- #define depthCueCoef 1.0
- /* diffuse reflection coefficient */
- #define diffReflCoef 5.0
- /* specular reflection coefficient */
- #define specReflCoef 5.0
- /* first light source intensity */
- #define light 1.0
- /* coefficient to approx highlight */
- #define highlightCoef 30
-
- char aChar;
- WindowPtr currentWindow;
- MenuHandle myMenus[editM+1];
- Rect dragRect, growRect;
- long newSize;
- Boolean doneFlag;
- EventRecord event;
- WindowPtr whichWindow;
- RGBColor pixColor;
- short i, j, k;
- PaletteHandle palH;
- DialogPtr dPtr;
- short doneDlg;
- OSErr err;
- SysEnvRec envRec;
-
- Rect copyRect;
- GWorldPtr wallyWorld;
- GDHandle savedDevice;
- CGrafPtr savedPort;
-
- double PowerOfN (double x, short r) {
- double ans;
-
- ans = 1.0;
- while (r-- > 0) ans *= x;
- return ans;
- }
-
- double fx, fy, fz;
-
- short CalcVolumeData (short i, short j, short k) {
- long x, y, z;
-
- fx = -(double)(i - halfVolSize);
- fy = -(double)(j - halfVolSize);
- fz = -(double)(k - halfVolSize);
- if ((fx * fx + fy * fy + fz * fz) <= volumeMag) return 1;
- else return 0;
- }
-
- void DoColor (short i, short j, short k, RGBColor *RGBVal) {
- double n_dot_h, n_dot_l, n_dot_h2, n_dot_l2, shade;
- unsigned short color;
-
- if (CalcVolumeData (i, j, k)) {
- n_dot_l = (fx + fy + fz)/sqrt3r;
- n_dot_h = (fx + fy + 2.7321*fz)/rsqrt6;
- shade = light*ambientReflCoef+(light/((double)(k)+depthCueCoef)*(diffReflCoef*n_dot_l+specReflCoef*PowerOfN (n_dot_h, highlightCoef)));
-
- /* second light source */
- n_dot_l2 = -fx/sphere_r;
- n_dot_h2 = (-fx + fz)/(1.4142*sphere_r);
- shade += light/((double)(k)+depthCueCoef)*(diffReflCoef*n_dot_l2+specReflCoef*PowerOfN (n_dot_h2, highlightCoef));
-
- color = (unsigned short)(shade * 65534.0);
- RGBVal->red = color;
- RGBVal->green = color;
- RGBVal->blue = color;
- }
- else {
- RGBVal->red = 0;
- RGBVal->green = 0;
- RGBVal->blue = 0;
- };
- }
-
- void OpenWindow (void) {
- currentWindow = (WindowPtr)GetNewCWindow(windowRscr, NULL, (Ptr)-1);
- SetPort(currentWindow);
- SizeWindow(currentWindow, volSize + 25, volSize + 25, 1);
- SetWTitle(currentWindow, &"\pVol3D");
- ShowWindow(currentWindow);
- }
-
- void Init (void) {
- short i, j, k;
-
- InitGraf(&thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (NULL);
-
- myMenus[appleM] = GetMenu(appleID);
- AddResMenu(myMenus[appleM], 'DRVR');
-
- myMenus[fileM] = GetMenu(fileID);
- myMenus[editM] = GetMenu(editID);
-
- for (i=appleM;i<=editM;i++) InsertMenu(myMenus[i], 0);
-
- DrawMenuBar ();
-
- SetRect(&dragRect, 30, 20, screenBits.bounds.right - 10, screenBits.bounds.bottom - 30);
- SetRect(&growRect, 50, 50, screenBits.bounds.right - 20, screenBits.bounds.bottom - 50);
-
- doneFlag = 0;
- err = SysEnvirons(1, &envRec);
- if (!envRec.hasColorQD) doneFlag = 1;
- else {
- OpenWindow ();
- palH = GetNewPalette (paletteRscr);
- if (palH == NULL) {
- doneFlag = 1;
- }
- else {
- SetPalette (currentWindow, palH, 1);
- }
-
- /* set up the offscreen drawing port */
- GetGWorld (&savedPort, &savedDevice);
- SetRect (©Rect, 0, 0, volSize-1, volSize-1);
- LocalToGlobal (©Rect.top);
- LocalToGlobal (©Rect.bottom);
- err = NewGWorld (&wallyWorld, 0, ©Rect, NULL, NULL, 0);
- GlobalToLocal (©Rect.top);
- GlobalToLocal (©Rect.bottom);
-
- if (err != noErr)
- doneFlag = 1;
- else {
- SetGWorld (wallyWorld, NULL);
- if (LockPixels (wallyWorld->portPixMap)) {
- /* draw off screen here */
- for(i=0;i<volSize;i++)
- for (j=0;j<volSize;j++) {
- k = 0;
- do {
- DoColor(i, j, k, &pixColor);
- k++;
- } while ((pixColor.red == 0) & (k < volSize));
- SetCPixel (i, j, &pixColor);
- }
- UnlockPixels (wallyWorld->portPixMap);
- }
- else doneFlag = 1;
-
- /* the drawing is done set the current port back to the display window */
- }
- SetGWorld (savedPort, savedDevice);
- }
- }
-
- void DoAboutBox (void) {
-
- dPtr = GetNewDialog (aboutDialog, NULL, (Ptr)-1);
- do
- ModalDialog(NULL, &doneDlg);
- while (!doneDlg);
- DisposDialog(dPtr);
- }
-
- void CleanUp (void) {
-
- HideWindow (currentWindow);
- DisposeGWorld (wallyWorld);
- DisposePalette (palH);
- DisposeWindow (currentWindow);
- doneFlag = 1;
- }
-
- void DoCommand (long menuResult) {
- short menuID, menuItem;
- Str255 daName;
- short daErr;
-
- menuItem = LoWord (menuResult);
- menuID = HiWord (menuResult);
-
- switch (menuID) {
- case appleID:
- if (menuItem == appleAbout) DoAboutBox ();
- else {
- GetItem(myMenus[appleM], menuItem, daName);
- daErr = OpenDeskAcc(daName);
- if (currentWindow) SetPort (currentWindow);
- }
- break;
- case fileID:
- switch (menuItem) {
- case fileQuit:
- CleanUp ();
- break;
- }
- break;
- }
- HiliteMenu(0);
- }
-
- void DoEvent (void) {
-
- switch (event.what) {
- case mouseDown:
- switch (FindWindow(event.where, &whichWindow)) {
- case inMenuBar:
- DoCommand(MenuSelect(event.where));
- break;
- case inSysWindow:
- SystemClick(&event, whichWindow);
- break;
- case inDrag:
- DragWindow(whichWindow, event.where, &dragRect);
- break;
- case inGrow:
- newSize = GrowWindow(whichWindow, event.where, &growRect);
- SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), 1);
- InvalRect(¤tWindow->portRect);
- break;
- case inGoAway:
- if (TrackGoAway(whichWindow, event.where)) CleanUp ();
- break;
- } /* case findwindow (...) */
- break;
- case keyDown:
- case autoKey:
- aChar = (char)(BitAnd (event.message, charCodeMask));
- if (BitAnd (event.modifiers, cmdKey)) DoCommand(MenuKey(aChar));
- break;
- case activateEvt:
- if (BitAnd(event.modifiers, activeFlag)) DisableItem(myMenus[editM], 0);
- else EnableItem(myMenus[editM], 0);
- break;
-
- case updateEvt:
- BeginUpdate(currentWindow);
- EraseRect(¤tWindow->portRect);
- DrawGrowIcon(currentWindow);
- InsetRect (¤tWindow->portRect, 8, 8);
- OffsetRect (¤tWindow->portRect, -8, -8);
-
- if (LockPixels (wallyWorld->portPixMap)) {
- CopyBits(&wallyWorld->portPixMap, ¤tWindow->portBits, ©Rect, ¤tWindow->portRect, srcCopy, NULL);
- UnlockPixels (wallyWorld->portPixMap);
- }
-
- OffsetRect (¤tWindow->portRect, 8, 8);
- InsetRect (¤tWindow->portRect, -8, -8);
- EndUpdate(currentWindow);
- break;
- }
- }
-
- void main (void) {
- currentWindow = NULL;
- Init ();
- InitCursor ();
-
- do {
- if (WaitNextEvent (everyEvent, &event, sleepTicks, NULL)) DoEvent ();
- } while (!doneFlag);
- }
-
-